package com.zd.common.bean;

import com.zd.common.enums.ResponseStatus;
import lombok.Data;

import java.io.Serializable;

/**
 * 自定义响应体
 * @author Pete
 * @since 2022/3/19
 */
@Data
public class ZdNovelResponse<T> implements Serializable {
    /*响应状态码*/
    private int code = ResponseStatus.OK.getCode();
    /*响应信息*/
    private String msg = ResponseStatus.OK.getMsg();

    private T data;

    private ZdNovelResponse(ResponseStatus responseStatus) {
        this.code = responseStatus.getCode();
        this.msg = responseStatus.getMsg();
    }

    private ZdNovelResponse(ResponseStatus responseStatus, T data) {
        this.code = responseStatus.getCode();
        this.msg = responseStatus.getMsg();
        this.data = data;
    }

    /**
     * 业务处理成功,无数据返回
     * */
    public static ZdNovelResponse<Void> ok(){
        return new ZdNovelResponse<Void>(ResponseStatus.OK);
    }

    /**
     * 业务处理成功,有数据返回
     * */
    public static <T> ZdNovelResponse<T> ok(T data) {
        return new ZdNovelResponse<T>(ResponseStatus.OK,data);
    }

    /**
     * 业务处理失败,无数据返回
     * */
    public static ZdNovelResponse<Void> fail(ResponseStatus ResponseStatus) {
        return new ZdNovelResponse<Void>(ResponseStatus);
    }

    /**
     * 业务处理失败,有数据返回
     * */
    public static <T> ZdNovelResponse<T> fail(ResponseStatus ResponseStatus,T data) {
        return new ZdNovelResponse<T>(ResponseStatus,data);
    }

    /**
     * 系统错误,无数据返回
     * */
    public static ZdNovelResponse<Void> error() {
        return new ZdNovelResponse<Void>(ResponseStatus.SYSTEM_ERROR);
    }
}
